//
// Created by Joey on 2019/12/15.
//

#include<iostream>
#include "../other/TerminalUtils.h"

using namespace std;
#define MAX 1000


void showMenu() {
    cout << "************************" << endl;
    cout << "***** 1.添加联系人 *****" << endl;
    cout << "***** 2.显示联系人 *****" << endl;
    cout << "***** 3.删除联系人 *****" << endl;
    cout << "***** 4.查找联系人 *****" << endl;
    cout << "***** 5.修改联系人 *****" << endl;
    cout << "***** 6.清空联系人 *****" << endl;
    cout << "***** 0.退出通讯录 *****" << endl;
    cout << "************************" << endl;
}

struct Person {
    string name;
    int sex;
    int age;
    string phone;
    string address;
};

struct AddressBook {
    // 通讯录中保存的联系人数组
    struct Person persons[MAX];
    // 通讯录中当前记录联系人个数
    int size = 0;
};

Person inputPerson() {
    string name = tipsAndInput("请输入姓名：");

    int sex;
    bool sexUnavailable = true;
    while (sexUnavailable) {
        sex = stoi(tipsAndInput("请输入性别：(1: 男  2: 女)"));
        sexUnavailable = !(sex == 1 || sex == 2);
        if (sexUnavailable) {
            cout << "输入有误，请重新输入" << endl;
        }
    }

    int age = stoi(tipsAndInput("请输入年龄："));

    string phone = tipsAndInput("请输入电话：");

    string address = tipsAndInput("请输入住址：");

    return Person{name, sex, age, phone, address};
}

void addPerson(AddressBook *abs) {
    if (abs->size >= MAX) {
        cout << "通讯录已满，无法继续添加！" << endl;
        return;
    }

    abs->persons[abs->size] = inputPerson();
    abs->size += 1;

    cout << "联系人添加成功😄" << endl;
}

void printPerson(Person person) {
    cout << "姓名：" << person.name
         << "\t性别：" << (person.sex == 1 ? "男" : "女")
         << "\t年龄：" << person.age
         << "\t电话：" << person.phone
         << "\t地址：" << person.address << endl;
}

void showPersons(AddressBook *abs) {
    if (abs->size <= 0) {
        cout << "当前记录为空" << endl;
        return;
    }

    for (int i = 0; i < abs->size; ++i) {
        printPerson(abs->persons[i]);
    }
}

int isPersonExist(AddressBook *abs, string personName) {
    for (int i = 0; i < abs->size; ++i) {
        Person person = abs->persons[i];
        if (person.name == personName) {
            return i;
        }
    }
    return -1;
}

void removePerson(AddressBook *abs, int position) {
    for (int i = position; i < abs->size; ++i) {
        abs->persons[i] = abs->persons[i + 1];
    }
    abs->size -= 1;
}

void deletePerson(AddressBook *abs) {
    if (abs->size <= 0) {
        cout << "当前记录为空" << endl;
        return;
    }

    string name = tipsAndInput("请输入要删除的联系人姓名：");
    int position = isPersonExist(abs, name);
    if (position > -1) {
        removePerson(abs, position);
        cout << "联系人已删除：" << name << endl;
    } else {
        cout << "查无此人：" << name << endl;
    }
}

void findPerson(AddressBook *abs) {
    if (abs->size <= 0) {
        cout << "当前记录为空" << endl;
        return;
    }

    string name = tipsAndInput("请输入要查找的联系人姓名：");
    int position = isPersonExist(abs, name);
    if (position > -1) {
        Person person = abs->persons[position];
        cout << "姓名：" << person.name
             << "\t性别：" << (person.sex == 1 ? "男" : "女")
             << "\t年龄：" << person.age
             << "\t电话：" << person.phone
             << "\t地址：" << person.address << endl;
    } else {
        cout << "查无此人：" << name << endl;
    }
}

void modifyPerson(AddressBook *abs) {
    if (abs->size <= 0) {
        cout << "当前记录为空" << endl;
        return;
    }

    string name = tipsAndInput("请输入要修改的联系人姓名：");
    int position = isPersonExist(abs, name);
    if (position > -1) {
        Person person = inputPerson();
        abs->persons[position] = person;
        cout << "联系人修改完成：" << name << endl;
        printPerson(person);
    } else {
        cout << "查无此人：" << name << endl;
    }
}

void cleanAddressBook(AddressBook *abs) {
    abs->size = 0;
    cout << "通讯录已清空！！" << endl;
}

int main() {

    int selection = 0;

    AddressBook abs;

    while (true) {
        showMenu();
        selection = stoi(tipsAndInput("请选择操作："));
        cleanConsole();
        switch (selection) {
            case 1:
                cout << "***** 1.添加联系人 *****" << endl;
                addPerson(&abs);
                waitForContinue();
                cleanConsole();
                break;
            case 2:
                cout << "***** 2.显示联系人 *****" << endl;
                showPersons(&abs);
                waitForContinue();
                cleanConsole();
                break;
            case 3:
                cout << "***** 3.删除联系人 *****" << endl;
                deletePerson(&abs);
                waitForContinue();
                cleanConsole();
                break;
            case 4:
                cout << "***** 4.查找联系人 *****" << endl;
                findPerson(&abs);
                waitForContinue();
                cleanConsole();
                break;
            case 5:
                cout << "***** 5.修改联系人 *****" << endl;
                modifyPerson(&abs);
                waitForContinue();
                cleanConsole();
                break;
            case 6:
                cout << "***** 6.清空联系人 *****" << endl;
                cleanAddressBook(&abs);
                waitForContinue();
                cleanConsole();
                break;
            case 0:
                cout << "***** 0.退出通讯录 *****" << endl;
                return 0;
            default:
                cout << "没有该选项" << endl;
                break;
        }
    }
}